from jili.calc import calcor_base
from scipy.stats import rankdata
import numpy as np
class rank(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["percent"]
        self.input = ["close"]
        self.parameters = {"timeperiod":20,"ascending":True}
        super().__init__(ta_arg)
        self.batch=self.parameters["timeperiod"]
        self.ascending=self.parameters["ascending"]
        self.lookback = self.batch - 1
    def calc(self):
        data=self.hisbars[self.input[0]]
        if self.ascending:
            percentile=rankdata(data)[np.where(data == data[-1])[0][0]]
        else:
            percentile = self.batch-rankdata(data)[np.where(data == data[-1])[0][0]]
        return percentile
